/*
** check_option.c for my_printf in /home/nadali_t//local/svn/tek2ed/my_printf
** 
** Made by thomas nadalie
** Login   <nadali_t@epitech.net>
** 
** Started on  Sun Nov 18 16:33:03 2012 thomas nadalie
** Last update Sun Nov 18 18:03:32 2012 thomas nadalie
*/

#include	<stdlib.h>
#include	<stdarg.h>
#include	"my.h"

int		check_flag(char format, va_list ap)
{
  void		(**functab)(va_list);
  char		*flagtab;
  int		i;

  i = 0;
  if ((functab = malloc(sizeof(*functab) * 9 + 1)) == NULL)
    my_put_error("Malloc failed in function check_flag\n");
  if ((flagtab = malloc(sizeof(*flagtab) * 9 + 1)) == NULL)
    my_put_error("Malloc failed in function check_flag\n");
  init_flagtab(flagtab);
  init_functab(functab);
  while (flagtab[i])
    {
      if (flagtab[i] == format)
	{
	  functab[i](ap);
	  return (1);
	}
      i++;
    }
  free(functab);
  free(flagtab);
  return (0);
}

int		catch_opt(const char *format, va_list ap)
{
  int		i;

  i = 0;
  while (format[i])
    {
      if (format[i] == '%')
  	{
	  i++;
  	  if ((check_flag(format[i], ap)) != 1)
	    return (1);
  	  i++;
  	}
      else
  	my_putchar(format[i++]);
    }
  return (0);
}
